﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Amazon.SimpleDB;
using Amazon.SimpleDB.Model;
using Amazon.SimpleDB.Util;

using System.Xml;
using System.IO;

namespace PopulatePennyStorm
{
    class Program
    {
        static void Main(string[] args)
        {
            string itemsDomain = "PennyStorm_Items";


            using (AmazonSimpleDBClient sdb = new AmazonSimpleDBClient("YOUR KEY", "YOUR SECRET"))
            {


                EnsureDomain(itemsDomain, sdb);

                //
                // Populate simple db
                //
                XmlDocument xml = new XmlDocument();
                xml.Load("auction_items.xml");
                XmlNode root = xml.SelectSingleNode("bidamigo_development");
                foreach (XmlNode auctionItem in root.ChildNodes)
                {
                    //
                    // The first node is a comment
                    //
                    if (auctionItem.HasChildNodes)
                    {
                        XmlNode id = auctionItem.SelectSingleNode("id");
                        XmlNode name = auctionItem.SelectSingleNode("item_name");
                        XmlNode image = auctionItem.SelectSingleNode("img_url");

                        //
                        // Put this into SimpleDB
                        //
                        PutAttributesRequest putAttributes = new PutAttributesRequest();

                        putAttributes.Attribute.Add(MakeAttribute("name", name.InnerText));
                        putAttributes.Attribute.Add(MakeAttribute("image", image.InnerText));
                        putAttributes.Attribute.Add(MakeAttribute("price", "0"));
                        putAttributes.Attribute.Add(MakeAttribute("time", "100"));
                        putAttributes.Attribute.Add(MakeAttribute("active", "0"));
                        putAttributes.Attribute.Add(MakeAttribute("touched", "0"));

                        putAttributes.DomainName = itemsDomain;
                        putAttributes.ItemName = id.InnerText;

                        try
                        {
                            sdb.PutAttributes(putAttributes);
                            Console.WriteLine("Added item");
                        }
                        catch (Exception exception)
                        {
                            Console.WriteLine(exception.ToString());
                        }
                    }
                }
            }
        }

        private static void EnsureDomain(string itemsDomain, AmazonSimpleDBClient sdb)
        {
            DeleteDomainRequest deleteDomain = new DeleteDomainRequest();
            deleteDomain.DomainName = itemsDomain;

            sdb.DeleteDomain(deleteDomain);

            CreateDomainRequest createDomain = new CreateDomainRequest();
            createDomain.DomainName = itemsDomain;

            sdb.CreateDomain(createDomain);
        }

        static ReplaceableAttribute MakeAttribute(string name, string value)
        {
            ReplaceableAttribute attribute = new ReplaceableAttribute();
            
            attribute.Name = name;
            attribute.Value = value;

            return attribute;
        }
    }
}
